function onPublish(handler)
    if not handler.genCode then return end
    handler.genCode = false --prevent default output

    fprint("Handling gen code in plugin")
    genCode(handler) --do it myself
end

-- this is copied from Editor-Install-Path/Resources/Data/StreamingAssets/Scripts
function genCode(handler) 
    local settings = handler.project:GetSettings("Publish").codeGeneration
    local codePkgName = handler:ToFilename(handler.pkg.name); --convert chinese to pinyin, remove special chars etc.
    local exportCodePath = handler.exportCodePath..'/'..codePkgName
    local namespaceName = codePkgName
    
    if settings.packageName~=nil and settings.packageName~='' then
        namespaceName = settings.packageName..'.'..namespaceName;
    end

    --CollectClasses(stripeMemeber, stripeClass, fguiNamespace)
    local classes = handler:CollectClasses(settings.ignoreNoname, settings.ignoreNoname)
    handler:SetupCodeFolder(exportCodePath, "cs") --check if target folder exists, and delete old files

    local getMemberByName = settings.getMemberByName

    local classCnt = classes.Count
    local writer = CodeWriter.new({ blockFromNewLine=false, usingTabs = true  })
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        local members = classInfo.members
        local references = classInfo.references
        writer:reset()

        -- local refCount = references.Count
        -- if refCount>0 then
        --     for j=0,refCount-1 do
        --         local ref = references[j]
        --         writer:writeln('using %s;', ref)
        --     end
        --     writer:writeln()
        -- end

        writer:writeln('using FairyGUI;')
        writer:writeln('using FairyGUI.Utils;')
        writer:writeln('namespace %s',namespaceName)
        writer:startBlock()
        writer:writeln('public partial class %s : %s', classInfo.className, classInfo.superClassName)
        writer:startBlock()

        local memberCnt = members.Count
        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            writer:writeln('public %s %s;', memberInfo.type, memberInfo.varName)
        end
        
        writer:writeln('public static string URL = "ui://%s%s";', handler.pkg.id, classInfo.resId)
        writer:writeln('public static string PackageName = "%s";', handler.pkg.name)
        writer:writeln()
        
        writer:writeln('public static %s CreateInstance()', classInfo.className)
        writer:startBlock()
        writer:writeln('return UIPackage.CreateObject(PackageName, "%s") as %s;', classInfo.resName, classInfo.className)
        writer:endBlock()
        writer:writeln()
        
        writer:writeln('public override void ConstructFromXML(XML xml)')
        writer:startBlock()
        writer:writeln()
        writer:writeln('base.ConstructFromXML(xml);')
        writer:writeln()
        for j=0,memberCnt-1 do
            local memberInfo = members[j]
            if memberInfo.group==0 then
                if getMemberByName then
                    writer:writeln('this.%s = this.GetChild("%s") as %s;', memberInfo.varName, memberInfo.name, memberInfo.type)
                else
                    writer:writeln('this.%s = this.GetChildAt(%s) as %s;', memberInfo.varName, memberInfo.index, memberInfo.type)
                end
            elseif memberInfo.group==1 then
                if getMemberByName then
                    writer:writeln('this.%s = this.GetController("%s");', memberInfo.varName, memberInfo.name)
                else
                    writer:writeln('this.%s = this.GetControllerAt(%s);', memberInfo.varName, memberInfo.index)
                end
            else
                if getMemberByName then
                    writer:writeln('this.%s = this.GetTransition("%s");', memberInfo.varName, memberInfo.name)
                else
                    writer:writeln('this.%s = this.GetTransitionAt(%s);', memberInfo.varName, memberInfo.index)
                end
            end
        end
        writer:endBlock()

        writer:endBlock() --class
        writer:endBlock()
        writer:save(exportCodePath..'/'..classInfo.className..'.cs')
    end

    writer:reset()

    local binderName = codePkgName..'Binder'

    writer:writeln('using Core;')
    writer:writeln('using FairyGUI;')
    writer:writeln('namespace %s',namespaceName)
    writer:startBlock()
    writer:writeln()
    writer:writeln('[Binder("%s")]', handler.pkg.name)
    writer:writeln('public class %s : IBinder', binderName)
    writer:startBlock()

    writer:writeln('public void BindAll()')
    writer:startBlock()
    for i=0,classCnt-1 do
        local classInfo = classes[i]
        writer:writeln('UIObjectFactory.SetPackageItemExtension(%s.URL, typeof(%s));', classInfo.className, classInfo.className)
    end
    writer:endBlock() --bindall

    writer:endBlock() --class

    writer:endBlock() --namespace
    
    writer:save(exportCodePath..'/'..binderName..'.cs')
end